﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MetadataBasedConfigArch.Web.MasterPage
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        #region Page Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #endregion

        #region Bind Data

        protected void BindData()
        {
            LoginHyperLink.NavigateUrl = Page.ResolveUrl("~/Login.aspx?RedirectUrl=" + Request.Url.ToString());
            if (Util.UserID > 0 && Util.TenantID > 0)
            {
                LoginHyperLink.Visible = false;
                UserHyperLink.Visible = true;

                using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                {
                    var user = (from u in entities.Users
                                where u.ID == Util.UserID && u.TenantID == Util.TenantID
                                select u).FirstOrDefault();
                    if (user != null)
                    {
                        UserHyperLink.Text = String.Format("Welcome {0}!", user.Name).ToString();
                    }
                }
            }
            else
            {
                UserHyperLink.Visible = false;
                LoginHyperLink.Visible = true;
                Response.Redirect("~/Login.aspx");
            }
        }

        protected bool CheckLogin()
        {
            return true;
        }

        #endregion
    }
}